package com.guyaning.deyson_ec.ec.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.guyaning.deyson_core.core.app.AccountManager;
import com.guyaning.deyson_core.core.app.IUserChecker;
import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.ui.Launcher.LauncherHolderCreator;
import com.guyaning.deyson_core.core.ui.Launcher.ScrollLauncherFlag;
import com.guyaning.deyson_core.core.utils.stroage.LattePreference;
import com.guyaning.deyson_ec.ec.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/5/2.
 */

public class LauncherScrollDelegate extends DeysonDelegate implements OnItemClickListener {

    private ConvenientBanner<Integer>  mConvenientBanners = null;

    private ILauncherListener mILauncherListener = null;

    private static final ArrayList<Integer>  INTEGERS = new ArrayList<>();

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener) {
            mILauncherListener = (ILauncherListener) activity;
        }
    }

    private void initBanner(){
        INTEGERS.add(R.mipmap.launcher_01);
        INTEGERS.add(R.mipmap.launcher_02);
        INTEGERS.add(R.mipmap.launcher_03);
        INTEGERS.add(R.mipmap.launcher_04);
        INTEGERS.add(R.mipmap.launcher_05);

        mConvenientBanners.setPages(new LauncherHolderCreator(),INTEGERS)
                          .setPageIndicator(new int[]{R.drawable.hot_normal,R.drawable.hot_focus})
                          .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                          .setOnItemClickListener(this)
                          .setCanLoop(false);
    }

    @Override
    public Object setLayout() {
        mConvenientBanners = new ConvenientBanner<Integer>(getContext());
        return mConvenientBanners;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
       initBanner();
    }

    @Override
    public void onItemClick(int position) {

        //第一次进入APP
        if(position == INTEGERS.size()-1){
            LattePreference.setAppFlag(ScrollLauncherFlag.HAS_FIRST_LAUNCHER_APP.name(),true);

            //检测用户是否登录了APP
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.SIGNED);
                    }
                }

                @Override
                public void onNotSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
                    }
                }
            });

        }
    }
}
